package com.dyava.commons.serialize;

import com.dyava.commons.lang.StringUtils;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;


public class YamlUtils {
    private static final String DEFAULT_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";
    private static YAMLMapper objectMapper;
    static {
        objectMapper = new YAMLMapper();
        objectMapper.setDateFormat(new SimpleDateFormat(DEFAULT_DATE_FORMAT));
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

        JavaTimeModule javaTimeModule = new JavaTimeModule();

        javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))   );
        javaTimeModule.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))   );
        javaTimeModule.addDeserializer(LocalTime.class,new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss"))   );

        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));

        objectMapper.registerModule(javaTimeModule);
    }

    public static<T> T toObject(String jsonStr,Class<T>clazz){
        if(StringUtils.isEmpty(jsonStr)){
            throw new SerializeRuntimeException("jsonStr 不能为空");
        }
        if(clazz==null){
            throw new SerializeRuntimeException("clazz 不能为空");
        }
        try {
            return (T)objectMapper.readValue(jsonStr, clazz);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeJsonMappingException(e.getMessage());
        }
    }

    public static ObjectNode toObject(String jsonStr){
        return  toObject(jsonStr,ObjectNode.class);
    }

    public static<T> T toObject(String jsonStr,TypeReference<T> type){
        if(StringUtils.isEmpty(jsonStr)){
            throw new SerializeRuntimeException("jsonStr 不能为空");
        }
        if(type==null){
            throw new SerializeRuntimeException("type 不能为空");
        }
        try {
            return (T)objectMapper.readValue(jsonStr, type);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeJsonMappingException(e.getMessage());
        }
    }

    public static<T> T toObject(String jsonStr,Type type){
        if(StringUtils.isEmpty(jsonStr)){
            throw new SerializeRuntimeException("jsonStr 不能为空");
        }
        if(type==null){
            throw new SerializeRuntimeException("type 不能为空");
        }
        try {
            JavaType javaType = objectMapper.getTypeFactory().constructType(type);
            return (T)objectMapper.readValue(jsonStr, javaType);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeJsonMappingException(e.getMessage());
        }
    }

    public static<T> T toObject(Object obj,Class<T>clazz){
        if(obj==null){
            throw new SerializeRuntimeException("obj 不能为空");
        }
        if(clazz==null){
            throw new SerializeRuntimeException("clazz 不能为空");
        }
        return (T)objectMapper.convertValue(obj, clazz);
    }

    public static<T> T toObject(Object obj,TypeReference<T>type){
        if(obj==null){
            throw new SerializeRuntimeException("obj 不能为空");
        }
        if(type==null){
            throw new SerializeRuntimeException("type 不能为空");
        }
        return (T)objectMapper.convertValue(obj, type);
    }

    public static<T> T toObject(Object obj,Type type){
        if(obj==null){
            throw new SerializeRuntimeException("obj 不能为空");
        }
        if(type==null){
            throw new SerializeRuntimeException("type 不能为空");
        }
        JavaType javaType = objectMapper.getTypeFactory().constructType(type);
        return (T)objectMapper.convertValue(obj, javaType);
    }

    public static<T> String toString(T obj){
        if(obj==null){
            throw new SerializeRuntimeException("obj 不能为空");
        }
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeJsonMappingException(e.getMessage());
        }
    }

    public static ObjectNode createObjectNode(){
        return objectMapper.createObjectNode();
    }

    public static ArrayNode createArrayNode(){
        return objectMapper.createArrayNode();
    }


}
